package multitype.loadmore;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.List;

public class LoadMoreDelegate {

    LoadMoreItem loadMoreItem = new LoadMoreItem();
    LoadMoreViewBinder loadMoreViewBinder = new LoadMoreViewBinder();




    public LoadMoreDelegate() {
    }

    public LoadMoreViewBinder getLoadMoreViewBinder() {
        return loadMoreViewBinder;
    }

    public void setLoadMoreViewBinder(LoadMoreViewBinder loadMoreViewBinder) {
        this.loadMoreViewBinder = loadMoreViewBinder;
    }

    public LoadMoreItem getLoadMoreItem() {
        return loadMoreItem;
    }

    public void setStatusLoadFail() {
        loadMoreItem.setLoadStatus(LoadMoreItem.LOAD_FAIL);
        loadMoreViewBinder.setStatus(loadMoreItem);
    }

    public void setStatusNoMore() {
        loadMoreItem.setLoadStatus(LoadMoreItem.LOAD_NO_MORE);
        loadMoreViewBinder.setStatus(loadMoreItem);
    }
    public void setStatusEmpty() {
        loadMoreItem.setLoadStatus(LoadMoreItem.LOAD_EMPTY);
        loadMoreViewBinder.setStatus(loadMoreItem);
    }

    public void setStatusLoading() {
        loadMoreItem.setLoadStatus(LoadMoreItem.LOADING);
        loadMoreViewBinder.setStatus(loadMoreItem);
    }
    public void setStatusNormal() {
        loadMoreItem.setLoadStatus(LoadMoreItem.LOAD_NORMAL);
        loadMoreViewBinder.setStatus(loadMoreItem);
    }

    public boolean isLoading() {
        return loadMoreItem.getLoadStatus() == LoadMoreItem.LOADING ? true : false;
    }

    public boolean isNoMore() {
        return loadMoreItem.getLoadStatus() == LoadMoreItem.LOAD_NO_MORE ? true : false;
    }

    public void addAllItem(List<Object> items, List<?> list) {
        if (items != null && list != null && list.size() > 0) {
            int l = items.size();
            if (l > 0) {
                l--;
                Object t = items.get(l);
                if (loadMoreItem.equals(t)) {
                    items.addAll(l, list);
                } else {
                    items.remove(loadMoreItem);
                    items.addAll(list);
                    items.add(loadMoreItem);
                }
            }else {
                items.addAll(list);
                items.add(loadMoreItem);
            }

        }
    }
    public void addLoadItemOnly(List<Object> items) {
        if (items != null ) {
            int l = items.size();
            if (l > 0) {
                l--;
                Object t = items.get(l);
                if (loadMoreItem.equals(t)) {

                } else {
                    items.remove(loadMoreItem);

                    items.add(loadMoreItem);
                }
            }else {

                items.add(loadMoreItem);
            }

        }
    }
    public void addItem(List<Object> items, Object item) {
        if (items != null && item != null) {
            int l = items.size();
            if (l > 0) {
                l--;
                Object t = items.get(l);
                if (loadMoreItem.equals(t)) {
                    items.add(l, item);
                } else {
                    items.remove(loadMoreItem);
                    items.add(item);
                    items.add(loadMoreItem);
                }
            }else {
                items.add(item);
                items.add(loadMoreItem);
            }

        }
    }
    /**
     * Should be called after recyclerView setup with its layoutManager and adapter
     *
     * @param recyclerView the RecyclerView
     */
    public void attach(RecyclerView recyclerView, LoadMoreSubject loadMoreSubject) {
        final LinearLayoutManager layoutManager
            = (LinearLayoutManager) recyclerView.getLayoutManager();
        recyclerView.addOnScrollListener(
                new EndlessScrollListener(loadMoreSubject, loadMoreItem,loadMoreViewBinder));
    }


    private static class EndlessScrollListener extends RecyclerView.OnScrollListener {

        private static final int VISIBLE_THRESHOLD = 0;
        private final LoadMoreSubject loadMoreSubject;
        LoadMoreItem loadMoreItem;
        LoadMoreViewBinder loadMoreViewBinder;

        View.OnClickListener onFailClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    loadMoreItem.setStatusLoading();
                loadMoreViewBinder.setStatus(loadMoreItem);
                //Log.e("onLoadMore",""+"failClick");
                loadMoreSubject.onLoadMore();
            }
        };



        private EndlessScrollListener(LoadMoreSubject loadMoreSubject, LoadMoreItem loadMoreItem,LoadMoreViewBinder loadMoreViewBinder) {

            this.loadMoreSubject = loadMoreSubject;
            this.loadMoreItem = loadMoreItem;
            this.loadMoreViewBinder = loadMoreViewBinder;
            loadMoreViewBinder.setOnFailClick(onFailClick);
        }


        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (!loadMoreSubject.canLoad()) {
                return;
            }
            if (dy < 0 || loadMoreItem.isLoading()) return;
            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            int visibleItemCount = recyclerView.getChildCount();
//            totalItemCount = layoutManager.getItemCount();
            int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
//
//            if (loading) {
//                if (totalItemCount > previousTotal) {
//                    loading = false;
//                    previousTotal = totalItemCount;
//                }
//            }
//            if (!loading
//                    && (totalItemCount - visibleItemCount) <= firstVisibleItem) {
//                currentPage++;
//                onLoadMore(currentPage);
//                loading = true;
//            }
            final int itemCount = layoutManager.getItemCount();
            final int lastVisiblePosition = layoutManager.findLastCompletelyVisibleItemPosition();
//            final boolean isBottom = (lastVisiblePosition >= itemCount - VISIBLE_THRESHOLD);
            final boolean isBottom = (itemCount - visibleItemCount) <= firstVisibleItem;
            //Log.e("onLoadMore","itemCount"+itemCount+"visibleItemCount"+visibleItemCount+"firstVisibleItem"+firstVisibleItem);

//            Log.e("count","---------------------------------");
//            Log.e("count","itemCount:"+itemCount);
//            Log.e("count","lastVisiblePosition:"+lastVisiblePosition);
//            Log.e("count","visibleItemCount:"+visibleItemCount);
//            Log.e("count","firstVisibleItem:"+firstVisibleItem);
//            Log.e("count","isBottom:"+isBottom);
//            Log.e("count","=================================");
            if (isBottom) {
                if (!loadMoreItem.isNoMore() && !loadMoreItem.isLoading() /*&& !loadMoreItem.isFail()*/) {
                    loadMoreItem.setStatusLoading();
                    loadMoreViewBinder.setStatus(loadMoreItem);
                    //Log.e("onLoadMore","isBottom"+isBottom);

                    loadMoreSubject.onLoadMore();
                }
            }
        }
    }


    public interface LoadMoreSubject {
        boolean canLoad();
        void onLoadMore();
    }
}
